package com.lxj.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationDetails;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author lxj
 * @date 2021/7/31 16:29
 * @Description:
 */
@RestController
@RequestMapping("sys/logout")
@Api(tags = "登出管理")
public class SysLogoutController {

    @Autowired
    private StringRedisTemplate redisTemplate;

    @PostMapping
    @ApiOperation("登出操作")
    public ResponseEntity<Void> doLogout() {
        // 把redis的token删除即可
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        OAuth2AuthenticationDetails details = (OAuth2AuthenticationDetails) authentication.getDetails();
        String jwt = details.getTokenValue();
        redisTemplate.delete("oauth:jwt:" + jwt);
        return ResponseEntity.ok().build();
    }

}
